package com.yx.filesystem.monitor;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Canvas;
import com.yx.filesystem.client.ServerNode;

public class ServerNodeDisplay extends Composite {
	Image serverOpenImage = SWTResourceManager.getImage(ServerNodeDisplay.class,
			"/com/yx/frame/builder/image/server_open.png");
	Image serverCloseImage = SWTResourceManager.getImage(ServerNodeDisplay.class,
			"/com/yx/frame/builder/image/server_close.png");
	ServerNode serverNode;
	Canvas canvas;
	Label storage;
	ProgressBar bl;
	Label title;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ServerNodeDisplay(Composite parent, int style,ServerNode node) {
		super(parent, SWT.BORDER);
		setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		this.serverNode=node;
		title = new Label(this, SWT.NONE);
		title.setForeground(SWTResourceManager.getColor(255, 20, 147));
		title.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		title.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		title.setBounds(94, 10, 226, 17);
		title.setText("");
		
		bl = new ProgressBar(this, SWT.NONE);
		bl.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		bl.setBackground(SWTResourceManager.getColor(SWT.COLOR_GREEN));
		bl.setBounds(94, 33, 211, 17);
		
		canvas = new Canvas(this, SWT.NONE);
		canvas.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		canvas.setBounds(10, 10, 84, 64);
		
		storage = new Label(this, SWT.NONE);
		storage.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
		storage.setText("");
		storage.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		storage.setBounds(94, 56, 226, 17);
		
		fileNumberTxt = new Label(this, SWT.WRAP);
		fileNumberTxt.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		fileNumberTxt.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_DARK_SHADOW));
		fileNumberTxt.setBounds(10, 80, 295, 170);
		
		canvas.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
            	if(serverNode.isAlive()){
            		e.gc.drawImage(serverOpenImage, 0, 0);
            	}else{
            		e.gc.drawImage(serverCloseImage, 0, 0);
            	}
            }
        });
		 refreashState();
	}
	static final double gb=1024*1024*1024;
	private Label fileNumberTxt;
	public void refreashState(){
		canvas.redraw();
		String tStr="未连接";
		if(serverNode.getId()!=null){
			tStr=serverNode.getId();
		}
		tStr=tStr+"（"+serverNode.getIp()+":"+serverNode.getPort()+"）：";
		this.title.setText(tStr);
		if(serverNode.isAlive()){
			title.setForeground(SWTResourceManager.getColor(255, 20, 147));
		}else{
			title.setForeground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY));
		}
		bl.setMinimum(0);
		bl.setMaximum(100);
		double tatolSpace=getDouble(serverNode.getState().getTotalSpace()/gb);
		double usedSpace=getDouble(serverNode.getState().getUsedSpace()/gb);
		double freeSpace=getDouble(serverNode.getState().getFreeSpace()/gb);
		int blValue=0;
		if(tatolSpace>0){
			blValue=(int)Math.rint(usedSpace*100/tatolSpace);			
		}
		bl.setSelection(blValue);
		String sStr=freeSpace+"GB可用，共"+tatolSpace+"GB";
		storage.setText(sStr);
		fileNumberTxt.setText("");
		List<String> backupNodes=serverNode.getState().getBackupNodes();
		if(backupNodes.size()>0){
			fileNumberTxt.setText(fileNumberTxt.getText()+"数据复制到：\r\n");
			for(int i=0;i<backupNodes.size();i++){
				fileNumberTxt.setText(fileNumberTxt.getText()+"  节点： "+backupNodes.get(i)+"\r\n");
			}
		}
		Hashtable<String, BigInteger> fileState=serverNode.getState().getFileState();
		if(serverNode.getId()!=null&&fileState.containsKey(serverNode.getId())){
			String fileNumber=fileState.get(serverNode.getId()).toString();
			fileNumberTxt.setText(fileNumberTxt.getText()+"本节点存储：\r\n");
			fileNumberTxt.setText(fileNumberTxt.getText()+"  存储（"+serverNode.getId()+"） : "+fileNumber+"\r\n");
		}
		Enumeration enums=fileState.keys();
		while(enums.hasMoreElements()){
			String nodeId=enums.nextElement().toString();
			String fileNumber=fileState.get(nodeId).toString();			
			if(!nodeId.equals(serverNode.getId())){
				fileNumberTxt.setText(fileNumberTxt.getText()+"  备份（"+nodeId+"） : "+fileNumber+"\r\n");
			}
		}
		
	}
	static double getDouble(double d){
		BigDecimal   b   =   new   BigDecimal(d);  
		return b.setScale(1,   BigDecimal.ROUND_HALF_UP).doubleValue();  
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
